package com.ja.openchat.ui.me;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.google.gson.JsonObject;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentSayHelloBinding;
import com.ja.openchat.ui.chat.ChatActivity;
import com.ja.openchat.ui.chat.MessageViewModel;
import com.ja.openchat.viewmodels.SayHelloViewModel;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.user.UserInfo;

import java.util.Map;

import me.goldze.mvvmhabit.base.BaseFragment;

//打个招呼~
public class SayHelloFragment extends BaseFragment<FragmentSayHelloBinding, SayHelloViewModel> {


    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_say_hello;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {

    }
    private long mUserId;
    @Override
    public void initData() {
        super.initData();
        Bundle bundle = getArguments();
        if (bundle != null) {
            mUserId = bundle.getLong("userId", UserInfo.getUserInfo().id);
        } else {
            mUserId = UserInfo.getUserInfo().id;
        }
    }

    @Override
    public void initView() {
        super.initView();
        binding.titleBar.setOnRightListener(v -> sendMsg());
    }

    private MessageViewModel messageViewModel;
    void sendMsg() {
        if(messageViewModel==null){
            messageViewModel = ViewModelProviders.of(this).get(MessageViewModel.class);
        }
        TelegramManager.getTelegramManager().createPrivateChat(getContext(), mUserId, new TgResultBlock<ChatInfo>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, ChatInfo obj) {
                if (obj != null) {
                    messageViewModel.sendTextMsg(obj,viewModel.content.get());
//                    Bundle bundle = new Bundle();
//                    bundle.putParcelable("conversation", obj);
//                    startActivity(ChatActivity.class, bundle);
                    ChatActivity.startActivity(getContext(),obj);

                }

            }

            @Override
            public void onTimeout(Map<String, Object> request) {

            }
        });
    }
}